from urllib import request, parse
from urllib.error import URLError
from http import cookiejar
from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener

url="http://www.baidu.com"
# -----------------------request模块，返回一个HTTPResponse类型的对象--------------------
print("-----------------------request.urlopen()模块--------------------")
response=None
try:
    # response=urllib.request.urlopen("www.baidu.com")
    response=request.urlopen(url)
except ValueError as e:
    print(e)
else:
    print(response.getheaders())
    print(response.getheader('server'))
    response.read()
    # 是bytes类型的数据，需要decode（解码）一下
    # print(response.read().decode('utf-8'))

# -----------------------使用urllib库的urlopen()方法有很大的局限性，比如不能设置响应头的信息等。所以需要引入Request()方法来包装请求--------------------
print("-----------------------request.Request()模块--------------------")
url = 'http://www.baidu.com/s?wd='
key = '路飞'
key_code = parse.quote(key)
url_all = url + key_code
headers={
    'User-Agent':'Mozilla/5.0 (compatible; MSIE 5.5; Windows NT)'
}

req=request.Request(url_all,headers=headers)
res=request.urlopen(req)
print(res.read().decode("utf-8"))


# -----------------------parse模块--------------------
print("-----------------------parse模块--------------------")
print(parse.urlencode({"name":"何伟","age":"18"},encoding="utf-8"))
result=parse.urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(type(result),result,sep='\n')  #返回的是一个元祖
print(result.scheme,result[0])  #可以通过属性或者索引来获取值
print(result.netloc,result[1])
print(result.path,result[2])
print(result.params,result[3])
print(result.query,result[4])
print(result.fragment,result[5])

# -----------------------模拟登陆--------------------
print("-----------------------模拟登陆--------------------")
username="17795179212"
password="586649hw"
login_url="https://gitee.com/login"


# 方式一
# 构造密码管理实例
p=HTTPPasswordMgrWithDefaultRealm()
# 添加用户名密码到密码管理实例中
p.add_password(None,login_url,username,password)
# 传递密码管理实例来构建认证实例
auth_handler=HTTPBasicAuthHandler(p)
# 构建一个Opener
opener=build_opener(auth_handler)
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
opener.addheaders=[headers]

try:
    response=opener.open(login_url)
    result=response.read().decode("utf-8")
    print(result)
except Exception as e:
    print(e)

# 方式二
# cookie_object = cookiejar.CookieJar()
# handler = request.HTTPCookieProcessor(cookie_object)
# opener1 = request.build_opener(handler)
#
# data={
# 'user[login]':"17795179212",
# 'user[password]':"586649hw"
# }
# req=request.Request(login_url,data=data)
# respon=opener1.open(req)
# opener1.addheaders=[headers]
# print(respon.read().encode("utf-8"))

